﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class SchoolsManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserInfo"] == null || ((Entity.UserInfo)Session["UserInfo"]).UserType != "admin")
                {
                    Response.Redirect("Default.aspx");
                }

                DataTable dt = new DataTable();
                SQLHelper.getSchools(out dt);
                gdvSchools.DataSource = dt;
            
                gdvSchools.DataKeyNames = new string[] {"Id"};
                gdvSchools.DataBind();
                Session.Remove("SchoolId");
                
            }
        }

        protected void gdvSchools_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            

            int i = Convert.ToInt32(gdvSchools.DataKeys[e.RowIndex].Value);
            SQLHelper.deleteSchoolById(i);
            SQLHelper.deleteTagBySchoolId(i);
            Response.Redirect("SchoolsManage.aspx");
        }

        protected void gdvSchools_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int i =Convert.ToInt32(gdvSchools.DataKeys[e.NewEditIndex].Value);
            Session.Add("SchoolId", i);
            Response.Redirect("SchoolEdit.aspx");

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("SchoolEdit.aspx");
        }

        protected void CourseManage_Command(object sender, CommandEventArgs e)
        {
            int i = Convert.ToInt32(e.CommandArgument);
            Session.Add("SchoolId", i);
            Response.Redirect("CourseManage.aspx");
        }

        protected void gdvSchools_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            Response.Write("<br>NewpageIndex: " + e.NewPageIndex);
            gdvSchools.PageIndex = e.NewPageIndex;
            DataTable dt = new DataTable();
            SQLHelper.getSchools(out dt);
            gdvSchools.DataSource = dt;

            gdvSchools.DataKeyNames = new string[] { "Id" };
            gdvSchools.DataBind();
        }

        
    }
}